﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WeChat.Common;
using BLL;
using WeChat.Common2;
using Newtonsoft.Json;

namespace WeChat.UserInfo
{
    public partial class PersonalInfo : System.Web.UI.Page
    {
        public string openid;
        public string state;
        public string cardNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //认证后得到code值
                if (Request.QueryString["code"] != null || Request.QueryString["code"] != "")
                {
                    string code = Request.QueryString["code"];
                    //ApplicationLog.Write("个人信息页面code:" + code);
                    if (Session["openid"] == null || Session["openid"].ToString() == "")
                    {
                        Session["openid"] = OAuthHelper.getOpenid(code);
                    }
                    openid = Session["openid"].ToString();
                    ApplicationLog.Write("个人信息界面获取到的openid:" + openid);
                    //验证是否绑定账号
                    if (new BindIdBLL().verifyId(openid))//已绑定过，查找个人信息并展示
                    {
                        try
                        {
                            GetUserInfoBLL gb = new GetUserInfoBLL();
                            DataSet ds = gb.getUserInfo(openid);
                            DataTable dt = ds.Tables[0];
                            ApplicationLog.Write($"个人信息={JsonConvert.SerializeObject(dt)}");
                            if ("管理卡".Equals(dt.Rows[0]["ryxm"].ToString()) || dt.Rows[0]["rfkh"].ToString() == "000001")
                            {
                                Response.Redirect("SYSCount.aspx?openid=" + openid);
                            }
                            name.Text = dt.Rows[0]["ryxm"] == null ? "" : dt.Rows[0]["ryxm"].ToString();//姓名
                        //cardname.Text = "卡号：" + dt.Rows[0]["rfkh"].ToString();//卡号
                            cardname.Text = dt.Rows[0]["rfkh"].ToString();//卡号
                            cardNum = dt.Rows[0]["rfkh"].ToString();
                            sex.Text = dt.Rows[0]["xb"].ToString();//性别
                            department.Text = dt.Rows[0]["bmmc"] == null ? "" : dt.Rows[0]["bmmc"].ToString();//部门
                            string rfye = dt.Rows[0]["rfye"] == null ? "" : dt.Rows[0]["rfye"].ToString();//余额
                            accounts.Text = float.Parse(rfye).ToString() + "元";
                            string yj = dt.Rows[0]["yj"].ToString() == "" ? "0" : dt.Rows[0]["yj"].ToString();//押金
                            deposit.Text = float.Parse(yj).ToString() + "元";
                            string btye = dt.Rows[0]["btye"].ToString() == "" ? "0" : dt.Rows[0]["btye"].ToString();//补贴余额
                            this.btye.Text = float.Parse(btye).ToString() + "元";
                            cardtype.Text = dt.Rows[0]["klb"].ToString();//卡类型
                            state = dt.Rows[0]["rfzt"].ToString();
                            cardState.Text = getCardState.getCardStates(Int32.Parse(state));//卡状态
                            var version = ds.Tables[1].Rows[0]["isfeat"].ToString().ToLower();
                            if (version == "true")
                            {
                                photoQuality.Text = getPhotoQuality((dt.Rows[0]["feat"].ToString()==""?"0": dt.Rows[0]["feat"].ToString()).Length).ToString();//照片质量

                            }
                            else
                            {
                                photoQuality.Text = "";
                            }
                        //string khrq = dt.Rows[0]["khrq"].ToString();//开户日期
                        //opendate.Text = DateTime.Parse(yxq).ToString("yyyy-MM-dd HH:mm:ss");

                            string yxq = dt.Rows[0]["yxq"].ToString();//开户日期
                            this.yxq.Text = DateTime.Parse(yxq).ToString("yyyy-MM-dd HH:mm:ss");
                            position.Text = dt.Rows[0]["zhiwu"].ToString();//职务
                            //if (dt.Rows[0]["zhiwu"].ToString() == "") { position.Text = "暂无"; }
                            tel.Text = dt.Rows[0]["dh"].ToString();//电话
                            string jf = "0";
                            if (dt.Rows[0]["jf"] != DBNull.Value)
                            {
                                jf = dt.Rows[0]["jf"].ToString();//积分
                            }
                            integral.Text = float.Parse(jf).ToString();
                        }
                        catch (Exception ex)
                        {
                            Response.Write("<span style='color:#FF0000;font-size:20px'>" + "报错了" + ex.Message + "</span>");
                        }

                    }
                    else//
                    {
                        string UntyingUrl = ConfigurationSettings.AppSettings["UntyingUrl"];
                        Response.Redirect(UntyingUrl);
                    }
                }
            }
        }

        private object getPhotoQuality(int length)
        {
            string rtn = string.Empty;
            if (length>5)
            {
                rtn = "合格";
            }
            else
            {
                rtn = "不合格，请重新采集";
            }

            return rtn;
        }
    }
}
